package shard

/**
 * Created by DJJ on 2019/12/27.
 */
package clustersharding.demo
import akka.actor.ActorSystem
import akka.cluster.sharding._
import clustersharding.shard.CalcShards
import com.typesafe.config.ConfigFactory
import shard.CalculatorShard.CalcCommands

object ClusterShardingDemo extends App {

  CalcShards.create(2551)
  CalcShards.create(0)
  CalcShards.create(0)
  CalcShards.create(0)

  Thread.sleep(1000)

  val shardingSystem = ActorSystem("ShardingSystem",ConfigFactory.load("sharding"))
  CalcShards.startupSharding(0,shardingSystem)

  Thread.sleep(1000)

  val calcRegion = ClusterSharding(shardingSystem).shardRegion(CalculatorShard.shardName)

  calcRegion ! CalcCommands("1012",Calculator.Num(13.0))   //shard 1, entity 1012
  calcRegion ! CalcCommands("1012",Calculator.Add(12.0))
  calcRegion ! CalcCommands("1012",Calculator.ShowResult)  //shows address too
  calcRegion ! CalcCommands("1012",Calculator.Disconnect)   //disengage cluster

  println("2012----------")
  calcRegion ! CalcCommands("2012",Calculator.Num(10.0))   //shard 2, entity 2012
  calcRegion ! CalcCommands("2012",Calculator.Mul(3.0))
  calcRegion ! CalcCommands("2012",Calculator.Div(2.0))
  calcRegion ! CalcCommands("2012",Calculator.Div(0.0))   //divide by zero


  Thread.sleep(15000)
  println("000000000000000000000")
  calcRegion ! CalcCommands("1012",Calculator.ShowResult)   //check if restore result on another node
  calcRegion ! CalcCommands("2012",Calculator.ShowResult)
  calcRegion ! CalcCommands("2012",Calculator.ShowResult)
}
